package com.allianz.webapps.secondController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.allianz.webapps.common.CustomerContextHolder;
import com.allianz.webapps.dbpojo.AzclCustomMenu;
import com.allianz.webapps.dbpojo.AzclMenuList;
import com.allianz.webapps.https.interfaces.type001.pojo.User;
import com.allianz.webapps.service.AzclCustomMenuService;
import com.allianz.webapps.service.AzclMenuListService;

@Controller
@RequestMapping(value="index/menu")
public class AppCustomMenuController {

	@Autowired
	private AzclMenuListService menuService;
	@Autowired
	private AzclCustomMenuService  customMenuService;
	
	 SimpleDateFormat dateFomtter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	 /**
	  * 获取全部菜单列表
	  * @param jsonParam
	  * @param session
	  * @return
	  */
	 @RequestMapping(value="getAllMenuList",method=RequestMethod.GET)
	 @ResponseBody
	 public Object  getMenuList(HttpSession session){
		 	Map<String,Object> map = new HashMap<String, Object>();
		 	//获取全部菜单
		 	CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_NEWTOUCH);
		 	List<AzclMenuList> allMenuList= menuService.getAllMenuList();
		 	map.put("menuList", allMenuList);
		 	map.put("status", "success");
		 	return map;
	 }
	 /**
	  * 根据用户获取菜单列表
	  * @param session
	  * @return
	  */
	 @RequestMapping(value="/getUserMenuList",method=RequestMethod.GET)
	 @ResponseBody
	 public Object getMenuListBuyUser(HttpSession session){
		 	Map<String,Object> map = new HashMap<String, Object>();
		 	User user = (User) session.getAttribute("username");
		 	try {
			 	CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_NEWTOUCH);
			 	List<AzclMenuList> clist  = new ArrayList<AzclMenuList>();
		 		List<AzclMenuList> list = customMenuService.getCustomList(user.getUserIntId());
		 		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_NEWTOUCH);
		 		List<AzclMenuList> slist = menuService.getNoHaveMenuList(list);
		 		map.put("indexList", list);
		 		clist.addAll(list);
		 		clist.addAll(slist);
		 		map.put("noList", clist);
				map.put("status", "success");
			} catch (Exception e) {
				e.printStackTrace();
				map.put("status", "error");
				map.put("msg", "获取数据失败,请稍后重试!");
			}
		 	return map;
	 }
	/**
	 * 添加/修改自定义菜单记录
	 * @param jsonParam
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/addAndUpdateCustomMenu")
	@ResponseBody
	public Object  addCustomMenu(String customList,HttpSession session){
		Map<String,Object> map = new HashMap<String, Object>();
	 String now=dateFomtter.format(new Date());
	try {
		User user = (User) session.getAttribute("username");
		if (StringUtils.isEmpty(customList)) {
			map.put("status", "error");
			 map.put("msg", "缺少必要参数!");
			return map;
		}
		
	 if (StringUtils.isEmpty(user.getUserIntId())) {
		 map.put("status", "error");
		 map.put("msg", "缺少必要参数!");
		return map;
	 	}
	 AzclCustomMenu menu = new AzclCustomMenu();
	 menu.setUserId(user.getUserIntId());
	 menu.setUserName(user.getAgentName());
	 menu.setCustomList(customList);
	 menu.setUpdateDate(now);
	 //查询用户是否已经自定义菜单
	 	CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_NEWTOUCH);
	 AzclCustomMenu  resultMenu = customMenuService.queryMenu(user.getUserIntId());
	 if (resultMenu ==null) {
		 //赋值 添加两个时间
		 menu.setCreateDate(now);
		 customMenuService.addMenu(menu);
		 map.put("status", "success");
		 map.put("msg", "自定义菜单成功!");
	}else{
	 	CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_NEWTOUCH);
		 int resultMenu2 = customMenuService.UpdateMenu(menu);
		 if (resultMenu2 >0) {
			 map.put("status", "success");
			 map.put("msg", "修改自定义菜单成功!");
		}
	}
		} catch (Exception e) {
			e.printStackTrace();
			 map.put("status", "error");
			 map.put("msg", "自定义菜单失败,请稍后重试!");
		}
	 return  map;
	}
	
}
